#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <bitset>
#include <vector>
#define int long long
#define pii pair <int, int>
using namespace std;
/* #ifdef ONLINE_JUDGE */

/* #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++) */
/* char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf; */

/* #endif */
int read() {
    int p = 0, flg = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') flg = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        p = p * 10 + c - '0';
        c = getchar();
    }
    return p * flg;
}
void write(int x) {
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}
bool _stmer;

#define fi first
#define se second

const int N = 8005, M = 5e6 + 5;

array <int, M> p;
bitset <M> vis;

int cnt;

void Euler(int n) {
    for (int i = 2; i <= n; i++) {
        if (!vis[i]) {
            cnt++;
            p[cnt] = i;
        }
        for (int j = 1; j <= cnt && i * p[j] <= n; j++) {
            vis[i * p[j]] = 1;
            if (i % p[j] == 0) break;
        }
    }
}

array <pii, N> s;
array <int, N> h;

void solve() {
    int n = read(), m = read();
    for (int i = 1; i <= n; i++)
        s[i].fi = read(), s[i].se = read();
    for (int i = 1; i <= n; i++)
        h[i] = read();
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            for (int k = j + 1; k <= n; k++) {
                vector <int> tp;
                tp.push_back(abs(s[i].fi - s[j].fi) + abs(s[i].se - s[j].se));
                tp.push_back(abs(s[i].fi - s[k].fi) + abs(s[i].se - s[k].se));
                tp.push_back(abs(s[k].fi - s[j].fi) + abs(s[k].se - s[j].se));
                sort(tp.begin(), tp.end());
                if ((tp[1] % 20 == 3 || tp[1] < 10) && !vis[tp[1]])
                    ans += h[i] + h[j] + h[k];
            }
        }
    }
    write(ans), puts("");
}

bool _edmer;
signed main() {
    cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
#ifndef cxqghzj
    freopen("chess.in", "r", stdin);
    freopen("chess.out", "w", stdout);
#endif
    Euler(5e6);
    int T = read();
    while (T--) solve();
    return 0;
}
